# -*- coding: utf-8 -*-
###############################################################################
# file    : views_bug_admin.py
# author  : wangyubin
# date    : Tue Apr 29 16:10:13 2014
# 
# brief   : views for bug管理
# history : init
###############################################################################

# uliweb lib
from uliweb import expose, functions
# python lib
import os
import datetime
# CM-box lib
from db_app.model_define import *
from comm_app.my_log import App_Log
from comm_app import utils

@expose('/admin')
class views_bug_admin(object):
    """ bug 管理
    """

    @expose('bug-list')
    def list(self):
        """ bug 一览页面
        """
        return {
            "projects": T_PROJECT.all(),
            "states": T_BUG_STATE.all(),
            "importances": T_BUG_IMPORTANCE.all()
        }


    @expose('get-bugs')
    def get_bugs(self):
        """ 根据条件获取bugs
        """

        # 检索条件
        bug_id         = request.POST.get("bug_id")
        bug_title      = request.POST.get("bug_title")
        bug_importance = request.POST.get("bug_importance")
        bug_state      = request.POST.get("bug_state")
        bug_project    = request.POST.get("bug_project")
        bug_startdate  = request.POST.get("bug_startdate") # format: yyyy-mm-dd
        bug_enddate    = request.POST.get("bug_enddate")   # format: yyyy-mm-dd

        bugs = T_BUG.all()
        if bug_id and bugs:
            App_Log.logger.debug(u'bug_id         : ' + bug_id)
            #bugs = bugs.get(int(bug_id))
            bugs = bugs.filter(T_BUG.c.id == bug_id)
        if bug_title and bugs:
            App_Log.logger.debug(u'bug_title      : ' + bug_title)
            bugs = bugs.filter(T_BUG.c.title.like("%" + bug_title + "%"))
        if bug_importance and bugs:
            App_Log.logger.debug(u'bug_importance : ' + bug_importance)
            bugs = bugs.filter(T_BUG.c.bug_importance == T_BUG_IMPORTANCE.c.id)\
                       .filter(T_BUG_IMPORTANCE.c.code == bug_importance)
        if bug_state and bugs:
            App_Log.logger.debug(u'bug_state      : ' + bug_state)
            bugs = bugs.filter(T_BUG.c.bug_state == T_BUG_STATE.c.id)\
                       .filter(T_BUG_STATE.c.code == bug_state)
        if bug_project and bugs:
            App_Log.logger.debug(u'bug_project    : ' + bug_project)
            bugs = bugs.filter(T_BUG.c.project == bug_project)
        if bug_startdate and bugs:
            App_Log.logger.debug(u'bug_startdate  : ' + bug_startdate)
            bugs = bugs.filter(T_BUG.c.update_time >= utils.str2datetime(bug_startdate, utils.DATE_FORMAT5))
        if bug_enddate and bugs:
            App_Log.logger.debug(u'bug_enddate    : ' + bug_enddate)
            bugs = bugs.filter(T_BUG.c.update_time <= utils.str2datetime(bug_enddate, utils.DATE_FORMAT5))

        dt = []
        if not bugs:
            return json({ "aaData": dt, })
            
        for bug in bugs:
            dr = [
                '<a href="/admin/bug-detail/' + str(bug.id) + '">' + str(bug.id) + '</a>',
                utils.fix_str_len(bug.title),
                bug.project.name,
                bug.bug_state.name,
                bug.bug_importance.name,
                utils.datetime2str(bug.update_time, utils.DATE_FORMAT5),
                bug.create_user.real_name,
            ]
            dt.append(dr)

        return json({ "aaData": dt, })


    @expose('bug-detail/<int:bug_id>')
    def detail(self, bug_id):
        """ 查看bug相关信息
        """

        bug = T_BUG.get(bug_id)
        return {
            "bug_title": bug.title,
            "bug_desc": bug.desc,
            "bug_project": bug.project.name,
            "bug_importance": bug.bug_importance.name,
            "bug_state": bug.bug_state.name,
            "assigned_user": bug.assign_to_user.real_name,
            "bug_attachments": bug.bug_attachments,
            "comments": bug.bug_comments,
            "bug_histories": bug.bug_histories.order_by(T_BUG_HISTORY.c.update_time),
        }

        
